%macro C 0
	enter 0,0
	push esi
	push edi
	push ebx
%endmacro
%macro endC 0
	push ebx
	push edi
	push esi
	leave
	ret
%endmacro

section .data
c_180: dd 180

section .text
extern malloc
extern free
global suma
global sumad
global mul
global areaCirculo
global potencia
global pot
global func
global func2
global fib
global dondeEstaLaAbejita
global rotarPuntos

suma:
	C
	finit
	fld dword [ebp+8]
	fadd dword [ebp+12]
	endC

sumad:
	C
	finit
	fld qword [ebp+8]
	fadd qword [ebp+16]
	endC

mul:
	C
	finit
	fld qword [ebp+8]
	fmul qword [ebp+16]
	endC

areaCirculo:
	C
	finit
	fldpi
	fld dword [ebp+8]
	fmul st0,st0
	fmul
	endC

potencia:
	C
	finit
	fild dword [ebp+16]
	fld qword [ebp+8]
	fyl2x
	fld st0
	frndint
	fsub st1,st0
	fxch st1
	f2xm1
	fld1
	faddp
	fscale
	endC

pot:
	C
	finit
	fld qword [ebp+16]
	fld qword [ebp+8]
	fyl2x
	fld st0
	frndint
	fsub st1,st0
	fxch st1
	f2xm1
	fld1
	faddp
	fscale
	endC

func:
	C
	finit
	fldpi					; pi
	fld dword [ebp+8]	; x, pi
	fld st0				; x, x, pi
	fadd st2				; x+pi, x, pi
	fmul st0				; (x+pi)**2, x, pi
	fxch st1				; x, (x+pi)**2, pi
	fsub st2				; x-pi, (x+pi)**2, pi
	fmul st0				; (x-pi)**2, (x+pi)**2, pi
	faddp					; (x-pi)**2+(x+pi)**2, pi
	fld1
	fadd st0
	fmul st0				; 4, (x-pi)**2+(x+pi)**2, pi
	fld1
	fld1
	faddp
	fmul st0,st1
	fld1
	fsubp					; 7, 4, (x-pi)**2+(x+pi)**2, pi
	fdivrp
	fmulp
	fcos					; cos((7/4)*(x-pi)**2+(x+pi)**2), pi
	fldl2e
	fld1
	fadd st0
	;pow
	fyl2x
	fld st0
	frndint
	fsub st1,st0
	fxch st1
	f2xm1
	fld1
	faddp
	fscale
	fstp st1				; e, cos((7/4)*(x-pi)**2+(x+pi)**2), pi
	;
	fmul st0				; e**2, cos((7/4)*(x-pi)**2+(x+pi)**2), pi
	fld1
	fld1
	faddp
	fld1
	fld1
	fld1
	faddp
	faddp
	fdivrp
	fmulp
	fmulp					; (3/2) e**2 cos((7/4)*(x-pi)**2+(x+pi)**2), pi
	fldz
	fsubrp				; -(3/2) e**2 cos((7/4)*(x-pi)**2+(x+pi)**2), pi
	fld dword [ebp+8]
	fsin
	fld dword [ebp+12]
	fcos
	fmulp
	fld1
	fld1
	faddp
	fmul st0
	fld1
	faddp
	fdivp st1,st0		; sinx*cosx/5, -(3/2) e**2 cos((7/4)*(x-pi)**2+(x+pi)**2), pi
	faddp
	fstp st1				; sinx*cosx/5 - (3/2) e**2 cos((7/4)*(x-pi)**2+(x+pi)**2)
	endC

func2:
	C
	finit
	mov edi,[ebp+8]
	mov esi,[ebp+12]
	xor ecx,ecx
	mov cx,[ebp+16]
	xor edx,edx
	fldz
	.loop:
		fld qword [edi+edx]
		fsin
		fmul st0
		fld qword [esi+edx]
		fcos
		fmul st0
		faddp
		fsqrt
		faddp
		lea edx,[edx+8]
	loop .loop
	endC

fib:
	C
	push dword 0
	push dword [ebp+8]
	finit
	fld1
	fld1
	fadd st0
	fmul st0
	fadd st1
	fsqrt
	fld st0
	fxch st2
	faddp
	fld1
	fadd st0
	fdivp		; T, sq5
	fild qword [esp]
	fld st0
	fld st2	; T, n, n, T, sq5
	;pow
	fyl2x
	fld st0
	frndint
	fsub st1,st0
	fxch st1
	f2xm1
	fld1
	faddp
	fscale
	fstp st1
	;
	fxch st2	; T, n, T^n, sq5
	fld1
	fsubp	; T-1, n, T^n, sq5
	;pow
	fyl2x
	fld st0
	frndint
	fsub st1,st0
	fxch st1
	f2xm1
	fld1
	faddp
	fscale
	fstp st1
	; (-(1-T))^n, T^n, sq5
	mov eax,[esp]
	shr eax,1
	jnc .ok
	fldz
	fsubrp
	.ok:	; ((1-T))^n, T^n, sq5
	fsubp
	fdivrp
	frndint
	pop eax
	fist dword [esp]
	pop eax
	endC

dondeEstaLaAbejita:
	C
	finit
	mov esi,[ebp+16]
	mov edi,[ebp+20]
	mov ebx,[ebp+24]
	fld dword [ebp+8]
	fld dword [ebp+12]
	fsincos		; cos, sin, r
	fmul st2		; rcos, sin, r
	fst dword [edi]
	fxch st2		; sin, r, rcos
	fmulp			; rsin, rcos
	fst dword [esi]
	fmulp			; x·y
	fld st0
	frndint		; int(xy), xy
	fsub st1,st0; int(xy), frac(xy)
	fxch st1		; frac(xy), int(xy)
	f2xm1			; 2^frac(xy)-1, int(xy)
	fld1
	faddp			; 2^frac(xy), int(xy)
	fscale		; 2^(frac(xy)+int(xy)), int(xy)
	fst dword [ebx]
	endC

rotarPuntos:
	C
	xor eax,eax
	mov ax,[ebp+16]
	sal eax,3
	push eax
	call malloc
	mov edi,eax
	pop eax
	mov esi,[ebp+8]
	finit
	fld dword [ebp+12]
	fldpi
	fmulp
	fild dword [c_180]
	fdivp
	fsincos
	fldz
	fld st2
	fsubp									; -sin, cos, sin
	fstp dword [esp-4]
	fstp dword [esp-8]
	fstp dword [esp-12]
	movlps xmm0,[esp-8]
	shufps xmm0,xmm0,01000100B 	; cos, -sin, cos, -sin
	movlps xmm1,[esp-12]
	shufps xmm1,xmm1,01000100B		; sin, cos, sin, cos
	xor ecx,ecx
	xor ebx,ebx
	mov cx,[ebp+16]
	shr ecx,1
	.loop:
		movups xmm2,[esi+ebx]		; x1,y1, x2,y2
		movups xmm3,xmm2
		mulps xmm2,xmm0				; cos x1,-sin y1, cos x2,-sin y2
		mulps xmm3,xmm1				; sin x1,cos y1, sin x2,cos y2
		haddps xmm2,xmm3				; cos x1-sin y1, cos x2-sin y2, sin x1+cos y1, sin x2+cos y2
		shufps xmm2,xmm2,11011000B	; cos x1-sin y1, sin x1+cos y1, cos x2-sin y2, sin x2+cos y2
		movups [edi+ebx],xmm2
		lea ebx,[ebx+16]
		loop .loop
	mov eax,edi
	endC
